package algotithm.weekendrace.leetcode226.test3;


import java.util.Arrays;

/**
 * @author zhouyanxiang
 * @Date 2021-01-2021/1/31-10:17
 * @Title 5667. 你能在你最喜欢的那天吃到你最喜欢的糖果吗？
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] cat = {7,4,5,3,8};
        int[][] qs = {{0,2,2},{4,2,4},{2,13,1000000000}};
        boolean[] b = solution.canEat(cat, qs);
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i] + " ");
        }

    }

    public boolean[] canEat(int[] cct, int[][] qs) {
        int cn = cct.length;
        int qn = qs.length;
        boolean[] ans = new boolean[qn];
        long[] ends = new long[cn];
        ends[0] = cct[0];
        for (int i = 1; i < cn; ++i) {
            ends[i] = ends[i-1]+cct[i];
        }
        for (int i = 0; i < qn; ++i) {
            int ft = qs[i][0];
            int fd = qs[i][1];
            long dc = qs[i][2];
            long minc = fd+1;
            long maxc = fd*dc+dc;
            int v1 = Arrays.binarySearch(ends,minc);
            if (v1<0) {
                v1++;
                v1=-v1;
            }
            int v2 = Arrays.binarySearch(ends,maxc);
            if (v2<0) {
                v2++;
                v2=-v2;
            }
            //System.out.println(ft+" "+fd+" "+dc+" "+minc+" "+maxc+" "+v1+" "+v2);
            ans[i] = v1<=ft&&ft<=v2;
        }
        return ans;
    }


}
